package com.gobygang.client.facebook;

import com.gobygang.client.Authenication.Provider;
import com.gobygang.client.Gobygang;
import com.gobygang.client.GobygangHandler;
import com.gobygang.client.GobygangPresenter;
import com.gobygang.client.Session;
import com.gobygang.shared.LoginInfo;
import com.gobygang.shared.TaxiGanguserInfo;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Provides responses for queries made 
 */
public class ThenGetUserDetails implements AsyncCallback<JavaScriptObject> {
	
	public ThenGetUserDetails(){
	}

	@Override
	public void onFailure(Throwable caught) {
		Window.alert("error"+caught.getMessage());
	}

	@Override
	public void onSuccess(JavaScriptObject result) {
		UserJso fbUser = (UserJso) result;
		LoginInfo fbloginInfo = Session.getInstance().getLoginInfo();
		String id = "";
		String name = "";
		if (!Gobygang.testing){
			id = fbUser.getId();
			name = fbUser.getFullName();
		} else {
			id = "12345";
			name = "tester";

		}

		fbloginInfo.setId(id);
		fbloginInfo.setLoginProvider(Provider.Facebook.toString());

		fbloginInfo.setLoggedIn(true);
		fbloginInfo.setNickname(name);
		fbloginInfo.setIsTempory(false);
		fbloginInfo.setEmailAddress(name+"@facebook.com");

		TaxiGanguserInfo taxiGanguserInfo = new TaxiGanguserInfo();
		taxiGanguserInfo.setId(id);
		
		Session.getInstance().setTaxiGangUserInfo(taxiGanguserInfo);
		Session.getInstance().setLoginInfo(fbloginInfo);
		GobygangPresenter.getInstance().authenticationComplete(true);
		GobygangHandler.getInstance().getTaxiGangUser();


	}
};
